#include <stdio.h> 
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <dirent.h>
#include <sys/stat.h> 
#include <sys/mount.h>
#include <sys/vfs.h>
#include <sys/statvfs.h>
#include <fcntl.h>
#include <pthread.h>
#include <errno.h>
#include "cjzconfig.h"
#include "hi_type.h"
#include "dbgprint.h"

#include "basic_datatype.h"

unsigned int watchdog_threadid;

#define TEST_WDG_NO	0

void wdg_clear_task(void *param)
{
    HI_S32 s32Ret;
	
    while(1)
    {
        sleep(5);
        s32Ret = HI_UNF_WDG_Clear(TEST_WDG_NO);
        if (HI_SUCCESS != s32Ret)
        {
            printf("%s: %d ErrorCode=0x%x\n", __FILE__, __LINE__, s32Ret);
        }
        else
        {
            printf("Clear wdg Success\n");
        }
    }

}

int watchdogModuleInit(void)
{
    HI_S32 s32Ret;
    HI_U32 u32Value;

    if (CJZ_RELEASE_CFG_VAL != getDebugCfg_wdgenable())
    {
        return OK;
    }
    /* Open WDG*/
    s32Ret = HI_UNF_WDG_Init();
    if (HI_SUCCESS != s32Ret)
    {
        printf("%s: %d ErrorCode=0x%x\n", __FILE__, __LINE__, s32Ret);
        return s32Ret;
    }

    /* Set WDG TimeOut */
    u32Value = 30000;    //ms
    s32Ret = HI_UNF_WDG_SetTimeout(TEST_WDG_NO, u32Value);
    if (HI_SUCCESS != s32Ret)
    {
        printf("%s: %d ErrorCode=0x%x\n", __FILE__, __LINE__, s32Ret);
        return s32Ret;
    }


    /* Enable WDG */
    s32Ret = HI_UNF_WDG_Enable(TEST_WDG_NO);
    if (HI_SUCCESS != s32Ret)
    {
        printf("%s: %d ErrorCode=0x%x\n", __FILE__, __LINE__, s32Ret);
        //return s32Ret;
    }

	pthread_create(&watchdog_threadid, NULL, wdg_clear_task, NULL);
   
	
    return OK;
}

int disableWdg()
{
    HI_S32 s32Ret;
    HI_U32 u32Loop;
    HI_U32 u32Value;

    /* Open WDG*/
    s32Ret = HI_UNF_WDG_Init();
    if (HI_SUCCESS != s32Ret)
    {
        printf("%s: %d ErrorCode=0x%x\n", __FILE__, __LINE__, s32Ret);
        return s32Ret;
    }
    s32Ret = HI_UNF_WDG_Disable(TEST_WDG_NO);
    if (HI_SUCCESS != s32Ret)
    {
        printf("%s: %d ErrorCode=0x%x\n", __FILE__, __LINE__, s32Ret);
        return s32Ret;
    }

}

/********************************************************************/






